<%-- 
    Document   : nextorend
    Created on : 2014/2/24, 下午 03:28:12
    Author     : Gjun
--%>

<%@page import="java.util.HashMap"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%
            boolean have=false;
            ArrayList<comm.comm> rsList=(ArrayList)session.getAttribute("rsList");
            if(rsList==null){
                 rsList=new ArrayList<comm.comm>();
            }
           if(request.getParameter("id")!=null){
            comm.comm com=(comm.comm)session.getAttribute("buy");            
            String id=request.getParameter("id");
            String addnum=request.getParameter("addnum");
            String pag=request.getParameter("pag");  
            com.setAddnum(Integer.parseInt(addnum));
            com.setId(Integer.parseInt(id));
            
            
            
            for(comm.comm c:rsList){
                    if(c.getId()==Integer.parseInt(id)){
                            c.setAddnum(c.getAddnum()+Integer.parseInt(addnum));
                            have=true;
                            break;
                    }
            }
            if(have==false){
                    rsList.add(com);                    
            }
            session.setAttribute("rsList",rsList);
            }
    %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
            <form  action="<%= response.encodeRedirectURL("del.jsp")%>"  id="OK" method="post">
                    <input type="hidden" id="count" name="count">
                    <input type="hidden" id="pag" name="pag">
            </form>
            <%
                    if(rsList.size()!=0){
                       
             %>
            <table border="2">
                <tr><td>商品名稱</td><td>單價</td><td>數量</td><td>總和</td><td></td></tr>
                 
            <%
                int i=0;                
                         for(comm.comm c:rsList)
                        {
                    
                                out.print("<tr><td>");
                                out.print(c.getName()+"</td><td>");
                                 out.print(c.getPrice()+"</td><td>");
                                 out.print(c.getAddnum()+"<td>");
                                out.print(c.getAddnum()*Integer.parseInt(c.getPrice())+"<td>");
                                out.print("<button  onclick=\"del("+i+")\">刪除</button>");
                                 i++;
                            }%>
                            </table>
                            <form action="<%= (session.getAttribute("user")==null)?"login.jsp":"endmay.jsp"%>">
                                <input type="submit" value="結帳" >
                                    <input type="hidden" value="${param.id}" name="id">
                            <input type="hidden" value="${param.num}" name="num">
        </form>
                  <%
                         
                          
                    }else{
                                        out.print("購物車裡無商品");
                                        }   
                
                    %>            
            

            

            <script>
                function del(i)
                { 
                    
                    
                    document.getElementById("count").value=i;
                    document.getElementById("pag").value=${param.pag};
                    document.getElementById("OK").submit();
                    
                }
                
                </script>
                

        <form action="daocy.jsp">
            <input type="submit" value="繼續購物">
            <input type="hidden" value="${param.pag}" name="pag">
        </form>      
    </body>
</html>
